package hu.jonas.time.client;

import hu.jonas.time.client.model.Project;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.tab.Tab;

public class ProjectPage {

	private static DynamicForm projectForm;
	private static ListGrid projectsGrid;
	
	public static Tab createProjectTab() {
		Tab tab = new Tab("Modify project");
		
		projectForm = new DynamicForm();
		projectForm.setWidth(300);
		
		TextItem name = new TextItem("projectName", "Project name");
		name.setRequired(true);
		
		DateItem dueDate = new DateItem("dueDate", "Due date");
		
		ButtonItem save = new ButtonItem("Save");
		save.setAlign(Alignment.CENTER);
		save.addClickHandler(new CreateProjectHandler());
		
		projectForm.setItems(name, dueDate, save);
		
		tab.setPane(projectForm);
		
		return tab;
	}
	
	public static Tab createProjectListTab() {
		Tab tab = new Tab("Projects");
		
		projectsGrid = new ListGrid();
		projectsGrid.setHeight100();
		projectsGrid.setWidth100();
		
		ListGridField name = new ListGridField("name", "Project name");
		ListGridField dueDate = new ListGridField("dueDate", "Due date");
		dueDate.setType(ListGridFieldType.DATE);
		
		projectsGrid.setFields(name, dueDate);
		
		tab.setPane(projectsGrid);
		
		TimeManagement.projectService.getProjects(new AsyncCallback<List<Project>>() {

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("Error during getting projects", caught);
			}

			@Override
			public void onSuccess(List<Project> result) {
				
				ListGridRecord[] projects = new ListGridRecord[result.size()];
				
				for (int i = 0; i < projects.length; i++) {
					projects[i] = result.get(i).toRecord(); 
				}

				projectsGrid.setData(projects);
			}
		});
		
		return tab;
	}
	
	private static class CreateProjectHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {

			String projectName = (String) projectForm.getValue("projectName");
			Date dueDate = (Date) projectForm.getValue("dueDate");
			
			TimeManagement.projectService.createProject(projectName, dueDate, new AsyncCallback<Long>() {

				@Override
				public void onFailure(Throwable caught) {
					SC.logWarn(caught.getMessage());
				}

				@Override
				public void onSuccess(Long result) {
					SC.say("Project successfully created with id: " + result);
				}
				
			});

		}
	}
	
}
